#include "receivethread.h"
#include <QMessageBox>
#include <QDebug>

#include "com.h"

ReceiveThread::ReceiveThread(QObject *parent) :
    QThread(parent)
{
    stop_flag = 1;
    baudChanged_flag = 0;
}

void ReceiveThread::run()
{
    qDebug()<<"Receive thread started";

    int count_receive=0, c=0;
    QString data;

    memset(buff, 0, 100);
    data.clear();

    while(stop_flag)
    {
        c = read(fd, buff, 100);

        if(baudChanged_flag)
        {
            data.clear();
            memset(buff, 0, 100);
            count_receive = 0;
            baudChanged_flag = 0;
            continue ;
        }

        if(c >= 0)
            data += buff;
        else continue;

        //memset(buff, 0, 100);
        //count_receive += c;
        count_receive=c;
        if(count_receive < 67)
            continue;

        //data = data.mid(1, 10);
        data = data.mid(0, 67);
        emit sendbuff(data);

        data.clear();
        count_receive = 0;
        memset(buff, 0, 100);
    }

    qDebug()<<"Receive thread exited";
}

void ReceiveThread::enStopFlag()
{
    stop_flag = 0;
}

void ReceiveThread::disStopFlag()
{
    stop_flag = 1;
}

void ReceiveThread::enBaudChanged_flag()
{
    baudChanged_flag = 1;
}
